package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"news/models"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	T, _ := c.GetInt("type")
	news := models.News{
		Title:  c.GetString("title"),
		Data:   c.GetString("data"),
		TypeId: T,
	}
	if T == 0 {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "分类不得为空",
		}
		c.ServeJSON()
		return
	}
	if news.Title == "" {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "标题不得为空",
		}
		c.ServeJSON()
		return
	}
	if news.Data == "" {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "内容不得为空",
		}
		c.ServeJSON()
		return
	}
	err := models.DB.Create(&news).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "添加成功",
	}
	c.ServeJSON()
}
func (c *NewsController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	var news models.News
	models.DB.Where("id = ? ", id).First(&news)
	if news.ID == 0 {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "删除目标不存在或已删除",
		}
		c.ServeJSON()
		return
	}
	err := models.DB.Delete(&news, id).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "删除成功",
	}
	c.ServeJSON()
}
func (c *NewsController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	if c.Ctx.Input.Method() == "GET" {
		var news models.News
		id, _ := c.GetInt("id")
		models.DB.Where("id = ?", id).First(&news)
		c.Data["json"] = models.Msg{
			Code: 200,
			Msg:  "",
			Data: news,
		}
		c.ServeJSON()
		c.StopRun()
	}
	T, _ := c.GetInt("type")
	news := models.News{
		Title:  c.GetString("title"),
		Data:   c.GetString("data"),
		TypeId: T,
	}
	err := models.DB.Create(&news).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "修改成功",
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var newses []models.News
	err := models.DB.Find(&newses).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: newses,
	}
	c.ServeJSON()
}
